from django.http import HttpResponse
from django.shortcuts import render


# Create your views here.
def index(request):
    # 使用快捷键 alt+enter
    # 看提示
    return HttpResponse('ok')


def register(request, mobile):
    print(request.method)

    return HttpResponse('注册')


def shop(request, pro_id, city_id):
    # print(pro_id,city_id)

    # import re
    # if not re.match('\d',pro_id):
    #     return HttpResponse('参数不正确')

    # 复用 [重用]

    ############查询字符串##############################
    query_params = request.GET
    # print(query_params)
    # <QueryDict: {'a': ['100'], 'b': ['200'], 'c': ['python']}>

    # 获取查询字符串的数据
    # a = query_params.get('a')
    # print(a)
    # print('~~~~~~~~~~~~')
    # b = query_params.get('b')
    # print(b)

    #########QueryDict################
    # QueryDict 获取一键多值 用 getlist
    # QueryDict 获取一键一值 用 get

    alist = query_params.getlist('a')
    print(alist)

    # get(key,default_value)
    # 如果 key 对应的数据 不存在,则使用默认值
    asingle = query_params.get('page', 1)
    print('~~~~~~~~~~~~~~~~~')
    print(asingle)

    return HttpResponse('我的小餐馆')


######POST -- 登录######################
def login(request):
    data = request.POST
    print(data)
    return HttpResponse('login')


#######POST  -- JSON数据######################
def get_json(request):
    # JSON数据 是非表单数据
    # 1. 接收数据
    bytes_body = request.body
    # print(bytes_body)
    # b'{\n\t"username":"itheima",\n\t"password":"1234567890"\n}'

    # 2. 将bytes类型的数据转换为 字符串
    body_str = bytes_body.decode()
    # print(body_str)
    """
       {
           "username":"itheima",
           "password":"1234567890"
       }
       <class 'str'>

       JSON形式的字符串
    """
    # print(type(body_str))
    # 3. 将字符串转换为 字典
    import json
    data = json.loads(body_str)
    # {'username': 'itheima', 'password': '1234567890'}
    # print(data)

    return HttpResponse('json')


########请求头##########################
def get_header_info(request):
    # 请求头
    print(request.META)
    # request.META 是字典数据

    print('~~~~~~~~~')
    print(request.META['CONTENT_TYPE'])

    # Content_Type
    # 内容类型
    # 告知 响应的客户端 我们返回的响应数据类型
    # content_type: 语法形式是: 大类/小类
    #  image/png    image/jpg      image/gif
    #  application/json
    # MIME类型

    # status 响应状态码 必须是 1xx~5xx

    # 1xx   消息
    # 2xx   成功
    # 3xx   重定向
    # 4xx   请求错误
    # 5xx   服务器错误
    # return HttpResponse('abc', content_type='text/html', status=200)

    data = {
        'name': 'abc'
    }
    import json
    data_str = json.dumps(data)

    return HttpResponse(data_str, content_type='application/json')


from django.http import HttpResponse
from django.http.response import HttpResponse

from django.http import JsonResponse


def weibo(request):
    data = [
        {
            'name': '如花',
            'age': 18
        },
        {
            'name': '如花',
            'age': 18
        }
    ]
    return JsonResponse(data, safe=False)


########cookie###########################
def set_cookie(request):
    # 模拟就是没有cookie
    # 服务器要设置cookie

    # 模拟设置一个username
    username = request.GET.get('username')

    response = HttpResponse('set_cookie')
    # 因为我们说的 cookie流程是 cookie数据的设置会保存在响应头中,所以要操作响应对象
    response.set_cookie(key='username', value=username, max_age=24 * 3600)
    response.set_cookie(key='age', value=10, max_age=24 * 3600)
    response.set_cookie(key='address', value='bj', max_age=24 * 3600)

    return response


def get_cookie(request):
    username = request.COOKIES.get('username')
    print(username)

    # xxx.objects.filter(username=username)

    return HttpResponse('get_cookie')
